const MYSQL = require("../../sql/mysql");
// MYSQL数据库配置信息
const MYSQLConfig = require("../../sql/database");
let sqlName = MYSQLConfig.datasheetName.roles;
/**
 * 获取角色列表
 * 
 */
async function RoleList(params = {}) {

}

/**
 *  获取角色列表（不分页）
 * 
 */
async function RoleListAll(params = {}) {
    try {
        let sql = `SELECT * FROM ${sqlName}`;

        let Con = await MYSQL.promise().getConnection()

        let list = await Con.query(sql);

        return {
            code:0,msg:"获取成功",data:list[0]
        }
    } catch (error) {
        
    }
}

/**
 * 修改角色信息
 * @param {object} params
 * @param {string} params.id 修改的角色id
 * @param {string} params.name 新的角色名称
 * @param {string} params.scope 新的权限标识
 * @param {string} params.state 新的state
 * @returns {Promise<{code:string,msg:string}>}
 */
async function updateRole(params = {}) {
    let { id, name, scope, comment, state } = params;
    if (!id) {
        throw {
            code: 500,
            msg: "id不能为空",
        };
    }
    let updateData = {};
    if (name) {
        updateData.name = name;
    }
    if (scope) {
        updateData.scope = scope;
    }
    if (comment) {
        updateData.comment = comment;
    }
    return {
        code: 0,
        msg: "操作成功",
        updateData,
    };
}

/**
* 删除角色 
* @param {object} params
* @param {string} params.id 角色id 
* @returns {Promise<{code:string,msg:string}>}
*/
async function deleteRole(params = {}) {
    try {
        let { id } = params;
        if (!id) {
            throw { code: 500, msg: "id不能为空" };
        }

        // 删除角色所有菜单权限
        // 删除角色所有操作权限

        // 删除角色
        let query = `DELETE FROM ${sqlName} WHERE id = ${id}`;
        await MYSQL.queryPromise(query);
        return {
            code: 0,
            msg: "删除成功",
        };
    } catch (error) {
        throw error;
    }
};

/**
 * 获取角色信息
 * @param {object} params
 * @param {string} [params.id] 角色id
 * @param {string} [params.name] 角色名称
 * @param {string} [params.scope] 角色标识
 * @return {Promise<{code:string,msg:string}>}
 */
async function roleInfo(params = {}) {
    try {
        let { id, name } = params;
        let query = `SELECT * FROM ${sqlName} WHERE `;
        if (id) {
            query += `id = ${id}`;
        } else if (name) {
            query += `name = '${name}'`;
        } else if (scope) {
            query += `scope = '${name}'`;
        } else {
            throw {
                code: 500,
                msg: "暂不支持该方式获取用户信息",
            };
        }
        try {
            let result = await MYSQL.promise().query(query);
            let list = result[0];
            if (list.length) {
                let info = list[0];
                return {
                    code: 0,
                    msg: "获取成功",
                    data: info,
                    exist: true,
                };
            } else {
                return {
                    code: 0,
                    msg: "获取成功",
                    data: [],
                    exist: false,
                };
            }
        } catch (err) {
            throw {
                code: 200,
                msg: "查询失败，数据库操作不成功",
            };
        }
    } catch (error) {
        throw error;
    }
};
/**
 * 添加角色
 * @param {object} params
 * @param {string} params.id 角色id
 * @param {string} params.menu 菜单id
 * @returns {Promise<{code:string,msg:string} | {code:string,msg:string}>}
 */
async function addRole(params = {}) {
    try {
        let { name, scope, comment } = params;
        if (!name) {
            throw { code: 500, msg: "角色名称不能为空" };
        } else if (!scope) {
            throw { code: 500, msg: "角色唯一标识不能为空" };
        }

        // 是否存在相同角色名称
        let existQuery = `
          SELECT * FROM ${sqlName} WHERE name = '${name}' or scope = '${scope}'
        `;
        let existResult = await MYSQL.promise().query(existQuery);
        let list = existResult[0]
        if (list.length) {

            let ifNameExist = list.some(i => i.name == name)
            let ifScopeExist = list.some(i => i.scope == scope)

            if (ifNameExist) {
                throw {
                    code: 500,
                    msg: "添加失败,角色名称:" + name + "已存在",
                };
            } else if (ifScopeExist) {
                throw {
                    code: 500,
                    msg: "添加失败,权限标识:" + scope + "已存在",
                };
            } else {
                throw {
                    code: 500,
                    msg: "添加失败,未知错误",
                };
            }


        }




        let create_date = DayJs(Date.now()).valueOf();
        await MYSQL.promise().query(`INSERT INTO ${sqlName} SET ?`, {
            name,
            scope,
            comment,
            create_date,
            state: 1,
        });
        return {
            code: 0,
            msg: "添加成功",
        };
    } catch (error) {
        throw error;
    }
};
/**
 * 删除角色菜单权限
 * @param {object} params
 * @param {string} params.id 角色id
 * @returns {Promise<{code:string,msg:string}>}
 */
async function deleteRoleMenusAuth(params = {}) { };
/**
 * 删除角色接口权限
 * @param {object} params
 * @param {string} params.id 角色id
 * @returns {Promise<{code:string,msg:string}>}
 */
async function deleteRoleApiAuth(params = {}) { };
/**
 * 删除角色操作权限
 * @param {object} params
 * @param {string} params.id 角色id
 * @returns {Promise<{code:string,msg:string}>}
 */
async function deleteRoleActiveAuth(params = {}) { };
module.exports = {
    RoleList,
    RoleListAll
}